package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.*;
import com.ruoyi.pos.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * orderController
 * 
 * @author wzh
 * @date 2021-04-19
 */
@RestController
@RequestMapping("/dev-api/system/pos/order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderItemService orderItemService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IRejectOrderService rejectOrderService;

    @Autowired
    private IRejectItemService rejectItemService;

    @Autowired
    private IExchangeOrderService exchangeOrderService;

    @Autowired
    private IExchangeItemService exchangeItemService;




    /**
     * 获取order详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderService.getInfo(id));
    }


    @GetMapping(value = "/code/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code) {
        return AjaxResult.success(orderService.getInfo(code));
    }
    /**
     * 生成订单
     */
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody Map<String, Long> map) {

        return AjaxResult.success(orderService.generate(map));
    }
    /**
     * 添加商品项
     * @param oid
     * @param ids
     * @return
     */
    @PostMapping("/addItem/{oid}")
    public AjaxResult addItem(@PathVariable Long oid,@RequestBody List<Long[]> ids) {
        return AjaxResult.success(orderService.addItem(oid, ids));
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @PostMapping("/generate_reject")
    public AjaxResult generateRejectOrder(@RequestBody  RejectOrder rejectOrder) {
//        生成退货时间
        rejectOrder.setRejectTime(new Date());
//        将对应退货订单状态设置退货状态
        Order order = orderService.selectOrderById(rejectOrder.getOrderId());
        order.setStatus(0);
        orderService.updateOrder(order);
        rejectOrderService.insertRejectOrder(rejectOrder);
        return AjaxResult.success(rejectOrder.getId());
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @PostMapping("/reject_item")
    public AjaxResult rejectItem(@RequestBody RejectItem rejectItem) {
//        计算退款商品总额
        rejectItem.setTotal(rejectItem.getRejectPrice().multiply(new BigDecimal(rejectItem.getQuantity())));
        rejectItemService.insertRejectItem(rejectItem);

//        恢复库存
        Goods goods = goodsService.selectGoodsByCode(rejectItem.getItemCode());
        goods.setInventory(goods.getInventory() + rejectItem.getQuantity().intValue());
        goodsService.updateGoods(goods);

//        计算退款订单总额
        RejectOrder rejectOrder = rejectOrderService.selectRejectOrderById(rejectItem.getRejectId());
        rejectOrder.setRejectAmount(rejectItem.getTotal());
        rejectOrderService.updateRejectOrder(rejectOrder);

        return AjaxResult.success(rejectItem.getId());
    }

    /**
     * 生成换货订单
     * @param exchangeOrder
     * @return
     */
    @PostMapping("/generate_exchange")
    public AjaxResult generateExchangeOrder(@RequestBody  ExchangeOrder exchangeOrder) {
//        生成退货时间
        exchangeOrder.setExchangeTime(new Date());
//        将对应退货订单状态设置退货状态
        Order order = orderService.selectOrderById(exchangeOrder.getOrderId());
        order.setStatus(2);
        orderService.updateOrder(order);
        exchangeOrderService.insertExchangeOrder(exchangeOrder);

        return AjaxResult.success(exchangeOrder.getId());
    }

    @PostMapping("/exchange_item")
    public AjaxResult exchangeItem(@RequestBody ExchangeItem exchangeItem) {
        exchangeItemService.insertExchangeItem(exchangeItem);
        return AjaxResult.success(exchangeItem.getId());
    }

    /**
     * 查询order列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {

        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出order列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:export')")
    @Log(title = "order", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "order");
    }




    /**
     * 新增order
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:add')")
    @Log(title = "order", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order).intValue());
    }

    /**
     * 修改order
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:edit')")
    @Log(title = "order", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除order
     */
//    @PreAuthorize("@ss.hasPermi('pos:order:remove')")
    @Log(title = "order", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }
}
